﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Unity;
using System.IO;

namespace MP3Ganizer.Modules.Visualisation.Views
{
    /// <summary>
    /// Interaction logic for VisualisationAudioV.xaml
    /// </summary>
    public partial class VisualisationAudioV : UserControl, IVisualisationAudioV
    {
        #region Fields
        private readonly IUnityContainer _container;
        #endregion

        /// <summary>
        /// Constructeur.
        /// </summary>
        /// <param name="container"></param>
        public VisualisationAudioV(IUnityContainer container)
        {
            this._container = container;
            InitializeComponent();
        }
        public VisualisationAudioV()
        {
            InitializeComponent();
        }

        public void SetModel(ViewModels.IVisualisationVM model)
        {
            this.DataContext = model;
        }

        public void Pause()
        {
            player.LoadedBehavior = MediaState.Manual;
            player.Pause();
            playerControl.IsChecked = false;
        }

        public void DisposeResources()
        {
            player.LoadedBehavior = MediaState.Manual;
            player.Close();
            //ViewModels.IVisualisationVM model = this.DataContext as ViewModels.IVisualisationVM;
            //model.Path = "";
        }

        private void playerControl_Click(object sender, RoutedEventArgs e)
        {
            player.LoadedBehavior = MediaState.Manual;
            if (playerControl.IsChecked != null && playerControl.IsChecked.Value)
            {
                TempAndPlay();
                //playMedia();
                
            }
            if (playerControl.IsChecked != null && !playerControl.IsChecked.Value)
            {
             player.Pause();
                //PauseMedia()
            }
        }

        //private void PauseMedia()
        //{
        //    throw new NotImplementedException();
        //}

        //private void playMedia()
        //{
        //    ViewModels.IVisualisationVM model = DataContext as ViewModels.IVisualisationVM;

        //    System.IO.FileStream fs = new System.IO.FileStream(model.Path, System.IO.FileMode.Open);
        //    long length = fs.Length;
        //    byte[] data = new byte[length];
        //    fs.Read(data, 0, (int)length);
        //    fs.Close();

        //    Stream ms = new MemoryStream();
        //    ms.Write(data, 0, (int)length);
        //    ms.Position = 0;

        //    NAudio.Utils.FileAssociations fa = new NAudio.Utils.FileAssociations();
        //    fa.


        //    WaveStream blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(ms)));

        //    using (Stream ms = new MemoryStream())
        //    {
        //        using (Stream stream = WebRequest.Create(url)
        //            .GetResponse().GetResponseStream())
        //        {
        //            byte[] buffer = new byte[32768];
        //            int read;
        //            while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
        //            {
        //                ms.Write(buffer, 0, read);
        //            }
        //        }

        //        ms.Position = 0;
        //        using (WaveStream blockAlignedStream =
        //            new BlockAlignReductionStream(
        //                WaveFormatConversionStream.CreatePcmStream(
        //                    new Mp3FileReader(ms))))
        //        {
        //            using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
        //            {
        //                waveOut.Init(blockAlignedStream);
        //                waveOut.Play();
        //                while (waveOut.PlaybackState == PlaybackState.Playing)
        //                {
        //                    System.Threading.Thread.Sleep(100);
        //                }
        //            }
        //        }
        //    }
   
        //}


        private void TempAndPlay()
        {
            ViewModels.IVisualisationVM model = DataContext as ViewModels.IVisualisationVM;
            if (!System.IO.File.Exists(model.Path))
                return;
            System.IO.FileStream st = new System.IO.FileStream(model.Path, System.IO.FileMode.Open);
            long length = st.Length;
            byte[] data = new byte[length];
            st.Read(data, 0, (int)length);
            st.Close();

            string name = string.Concat("./temp/", Guid.NewGuid().ToString(), ".mp3");
            System.IO.FileStream fs = new System.IO.FileStream(name, System.IO.FileMode.OpenOrCreate);
            fs.Write(data, 0, (int)length);
            fs.Flush();
            fs.Close();
            
            player.Source = new Uri(name, UriKind.Relative);
            player.Play();
        }

        private void stopControl_Click(object sender, RoutedEventArgs e)
        {
            player.LoadedBehavior = MediaState.Manual;
            player.Stop();
            player.LoadedBehavior = MediaState.Pause;
            playerControl.IsChecked = false;
            //StopMedia();
        }

        private void StopMedia()
        {
            throw new NotImplementedException();
        }
    }
}
